package com.joyxy.core;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

/**
 * 机器人控制器 - 封装Java Robot API
 * 提供鼠标和键盘操作功能
 */
public class RobotController {
    private static final Logger logger = LoggerFactory.getLogger(RobotController.class);
    
    private final Robot robot;
    
    public RobotController() throws AWTException {
        this.robot = new Robot();
        this.robot.setAutoDelay(50); // 设置操作间延迟
        
        logger.info("机器人控制器初始化完成");
    }
    
    /**
     * 点击指定坐标
     */
    public void click(int x, int y) {
        logger.debug("点击坐标: ({}, {})", x, y);
        
        robot.mouseMove(x, y);
        robot.delay(100);
        robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
        robot.delay(50);
        robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
    }
    
    /**
     * 右键点击
     */
    public void rightClick(int x, int y) {
        logger.debug("右键点击坐标: ({}, {})", x, y);
        
        robot.mouseMove(x, y);
        robot.delay(100);
        robot.mousePress(InputEvent.BUTTON3_DOWN_MASK);
        robot.delay(50);
        robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);
    }
    
    /**
     * 双击
     */
    public void doubleClick(int x, int y) {
        logger.debug("双击坐标: ({}, {})", x, y);
        
        robot.mouseMove(x, y);
        robot.delay(100);
        
        for (int i = 0; i < 2; i++) {
            robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
            robot.delay(30);
            robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
            robot.delay(30);
        }
    }
    
    /**
     * 拖拽操作
     */
    public void drag(int startX, int startY, int endX, int endY) {
        logger.debug("拖拽: ({}, {}) -> ({}, {})", startX, startY, endX, endY);
        
        robot.mouseMove(startX, startY);
        robot.delay(100);
        robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
        robot.delay(200);
        
        // 平滑移动
        int steps = 20;
        for (int i = 1; i <= steps; i++) {
            int x = startX + (endX - startX) * i / steps;
            int y = startY + (endY - startY) * i / steps;
            robot.mouseMove(x, y);
            robot.delay(10);
        }
        
        robot.delay(100);
        robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
    }
    
    /**
     * 移动鼠标到指定位置
     */
    public void moveTo(int x, int y) {
        robot.mouseMove(x, y);
    }
    
    /**
     * 按下键盘按键
     */
    public void keyPress(int keyCode) {
        logger.debug("按下按键: {}", KeyEvent.getKeyText(keyCode));
        robot.keyPress(keyCode);
        robot.delay(50);
        robot.keyRelease(keyCode);
    }
    
    /**
     * 输入文本
     */
    public void type(String text) {
        logger.debug("输入文本: {}", text);
        
        for (char c : text.toCharArray()) {
            typeChar(c);
        }
    }
    
    /**
     * 输入单个字符
     */
    private void typeChar(char c) {
        switch (c) {
            case 'a': keyPress(KeyEvent.VK_A); break;
            case 'b': keyPress(KeyEvent.VK_B); break;
            case 'c': keyPress(KeyEvent.VK_C); break;
            case 'd': keyPress(KeyEvent.VK_D); break;
            case 'e': keyPress(KeyEvent.VK_E); break;
            case 'f': keyPress(KeyEvent.VK_F); break;
            case 'g': keyPress(KeyEvent.VK_G); break;
            case 'h': keyPress(KeyEvent.VK_H); break;
            case 'i': keyPress(KeyEvent.VK_I); break;
            case 'j': keyPress(KeyEvent.VK_J); break;
            case 'k': keyPress(KeyEvent.VK_K); break;
            case 'l': keyPress(KeyEvent.VK_L); break;
            case 'm': keyPress(KeyEvent.VK_M); break;
            case 'n': keyPress(KeyEvent.VK_N); break;
            case 'o': keyPress(KeyEvent.VK_O); break;
            case 'p': keyPress(KeyEvent.VK_P); break;
            case 'q': keyPress(KeyEvent.VK_Q); break;
            case 'r': keyPress(KeyEvent.VK_R); break;
            case 's': keyPress(KeyEvent.VK_S); break;
            case 't': keyPress(KeyEvent.VK_T); break;
            case 'u': keyPress(KeyEvent.VK_U); break;
            case 'v': keyPress(KeyEvent.VK_V); break;
            case 'w': keyPress(KeyEvent.VK_W); break;
            case 'x': keyPress(KeyEvent.VK_X); break;
            case 'y': keyPress(KeyEvent.VK_Y); break;
            case 'z': keyPress(KeyEvent.VK_Z); break;
            case ' ': keyPress(KeyEvent.VK_SPACE); break;
            case '\n': keyPress(KeyEvent.VK_ENTER); break;
            default:
                if (Character.isDigit(c)) {
                    keyPress(KeyEvent.VK_0 + (c - '0'));
                }
                break;
        }
        robot.delay(30);
    }
    
    /**
     * 组合键操作
     */
    public void keyCombo(int... keyCodes) {
        logger.debug("组合键操作");
        
        // 按下所有键
        for (int keyCode : keyCodes) {
            robot.keyPress(keyCode);
            robot.delay(10);
        }
        
        robot.delay(100);
        
        // 释放所有键（反向顺序）
        for (int i = keyCodes.length - 1; i >= 0; i--) {
            robot.keyRelease(keyCodes[i]);
            robot.delay(10);
        }
    }
    
    /**
     * 设置延迟时间
     */
    public void setDelay(int delayMs) {
        robot.setAutoDelay(delayMs);
    }
    
    /**
     * 等待指定时间
     */
    public void delay(int ms) {
        robot.delay(ms);
    }
} 